package org.jeecg.modules.fk.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.system.vo.SysUserCacheInfo;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.fk.entity.*;
import org.jeecg.modules.fk.entity.req.DjUserAssignRequest;
import org.jeecg.modules.fk.mapper.*;
import org.jeecg.modules.fk.service.IDjUserInfoService;
import org.jeecg.modules.qyxx.entity.CompanyInfo;
import org.jeecg.modules.qyxx.mapper.CompanyInfoMapper;
import org.jeecg.modules.qyxx.util.DjLoginUtil;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Description: dj_user_org_ext
 * @Author: ZJJ
 * @Date: 2021-05-14
 * @Version: V1.0
 */
@Service
public class DjUserInfoServiceImpl extends ServiceImpl<DjUserInfoMapper, DjUserInfo> implements IDjUserInfoService {
    @Autowired
    private DjUserInfoMapper djUserInfoMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private DjRealNameMapper djRealNameMapper;
    @Autowired
    private DjUserMapper djUserMapper;
    @Autowired
    private DjRealNameOrgMapper djRealNameOrgMapper;
    @Autowired
    private DjUserRealNameMapper djUserRealNameMapper;
    @Autowired
    private DjOrgMapper djOrgMapper;
    @Autowired
    private ISysDepartService sysDepartService;
    @Autowired
    private CompanyInfoMapper companyInfoMapper;


/*    @Override
    public IPage<DjUserInfo> queryPageList(DjUserInfo djUserInfo, Integer pageNo, Integer pageSize, HttpServletRequest req) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Set<String> roleNameListString = sysUserService.getUserRolesSet(loginUser.getUsername());
        List<String> sysMultiOrgCode = new ArrayList<>();
        // 如果该用户为超级管理员
        if (!roleNameListString.contains("superAdmin")) {
            // 判断用户是否为某部门管理员
            if (!roleNameListString.contains("person")) {
                SysUserCacheInfo cacheUser = sysUserService.getCacheUser(loginUser.getUsername());
                sysMultiOrgCode = cacheUser.getSysMultiOrgCode();
                if (sysMultiOrgCode.size() > 0) {
                    for (int i = sysMultiOrgCode.size() - 1; i >= 0; i--) {
                        if (sysMultiOrgCode.get(i).contains(cacheUser.getSysOrgCode())) {
                            sysMultiOrgCode.remove(i);
                        }
                    }
                }
                if (sysMultiOrgCode.size() == 1) {
                    djUserInfo.setSaleNetCode(cacheUser.getSysOrgCode());
                }
            }
        }

        QueryWrapper<DjUserInfo> queryWrapper = QueryGenerator.initQueryWrapper(djUserInfo, req.getParameterMap());
        Page<DjUserInfo> page = new Page<DjUserInfo>(pageNo, pageSize);
        IPage<DjUserInfo> pageList = this.page(page, queryWrapper);
        List<DjUserInfo> records = pageList.getRecords();
        if (records != null && records.size() != 0) {
            if (sysMultiOrgCode.size() > 1) {
                for (int i = 1; i < sysMultiOrgCode.size(); i++) {
                    records.addAll(djUserInfoMapper.getNsrxxBySaleNetCode(sysMultiOrgCode.get(i)));
                }
            }
            List<String> djUserIds = djUserInfoMapper.getDjUserIds(loginUser.getId());
            // 当前用户为普通员工时,只获取该用户的客户
            if (roleNameListString.contains("person") && djUserIds.size() > 0) {
                records = djUserInfoMapper.selectBatchIds(djUserIds);
            }
            if (StringUtils.hasLength(djUserInfo.getNsrmc())) {
                String nsrmc = djUserInfo.getNsrmc();
                List<String> nsrsbhs = djUserInfoMapper.getNsrsbhByNsrmc(nsrmc);
                records = new ArrayList<>();
                for (String nsrsbh : nsrsbhs) {
                    records.add(djUserInfoMapper.getNsrxxByNsrsbh(nsrsbh));
                }
            }
            for (DjUserInfo record : records) {
                record.setNsrmc(djUserInfoMapper.getNsrmcByNsrsbh(record.getNsrsbh()));
            }
        }
        return pageList;
    }*/

/*    @Override
    public List<DjUserInfo> listForPage(DjUserInfo djUserInfo) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Set<String> roleNameListString = sysUserService.getUserRolesSet(loginUser.getUsername());
        List<String> sysMultiOrgCode = new ArrayList<>();
        // 如果该用户为超级管理员
        if (!roleNameListString.contains("superAdmin")) {
            // 判断用户是否为某部门管理员
            if (!roleNameListString.contains("person")) {
                djUserInfo.setKeyWord(loginUser.getOrgCode());
            } else {// 当前用户为普通员工时,只获取该用户的客户
                List<String> djUserIds = djUserInfoMapper.getDjUserIds(loginUser.getId());
                if (djUserIds != null && djUserIds.size() > 0) {
                    djUserInfo.setIds(djUserIds);
                } else {
                    djUserInfo.setSaleNetCode("-1");
                }
            }
        }
        return djUserInfoMapper.listForPage(djUserInfo);
    }*/

    /*@Override
    public void saveDjUserInfo(DjCheckBO djCheckBO, YhxxDTO yhxxDTO, CompanyInfo djNsrxx) {
        LambdaQueryWrapper<DjUserInfo> wrapper = Wrappers.lambdaQuery(DjUserInfo.class);
        wrapper.eq(DjUserInfo::getNsrsbh, djCheckBO.getNsrsbh());
        DjUserInfo one = djUserInfoMapper.selectOne(wrapper);
        if (one == null) {
            DjUserInfo djUserInfo = new DjUserInfo();
            BeanUtils.copyProperties(djCheckBO, djUserInfo);
            djUserInfo.setRealNameId(yhxxDTO.getSmxxid());
            djUserInfo.setUserId(yhxxDTO.getYhid());
            djUserInfo.setMobile(djCheckBO.getPhone());
            djUserInfo.setCreateTime(new Date());
            djUserInfo.setUpdateTime(new Date());
            djUserInfoMapper.insert(djUserInfo);

            if (djRealNameMapper.selectById(djUserInfo.getRealNameId()) == null) {
                DjRealName djRealName = new DjRealName();
                djRealName.setCreateTime(new Date());
                djRealName.setUpdateTime(new Date());
                djRealName.setPassword(djCheckBO.getPassword());
                djRealName.setRealName(yhxxDTO.getXm());
                djRealName.setRealNameId(djUserInfo.getRealNameId());
                djRealNameMapper.insert(djRealName);
            }

            DjUser djUser = new DjUser();
            djUser.setCreateTime(new Date());
            djUser.setUpdateTime(new Date());
            djUser.setMobile(yhxxDTO.getSjh());
            djUser.setUserId(yhxxDTO.getYhid());
            djUser.setUserName(yhxxDTO.getYhm());
            djUserMapper.insert(djUser);

            DjRealNameOrg djRealNameOrg = new DjRealNameOrg();
            djRealNameOrg.setCreateTime(new Date());
            djRealNameOrg.setUpdateTime(new Date());
            djRealNameOrg.setNsrsbh(djCheckBO.getNsrsbh());
            djRealNameOrg.setRealNameId(yhxxDTO.getSmxxid());
            djRealNameOrgMapper.insert(djRealNameOrg);

            DjUserRealName djUserRealName = new DjUserRealName();
            djUserRealName.setCreateTime(new Date());
            djUserRealName.setUpdateTime(new Date());
            djUserRealName.setReadNameId(yhxxDTO.getSmxxid());
            djUserRealName.setUserId(yhxxDTO.getYhid());
            djUserRealNameMapper.insert(djUserRealName);

            DjOrg djOrg = new DjOrg();
            djOrg.setCreateTime(new Date());
            djOrg.setNsrmc(djCheckBO.getNsrmc());
            djOrg.setNsrsbh(djCheckBO.getNsrsbh());
            djOrg.setUpdateTime(new Date());
            djOrg.setShxydm(djNsrxx.getShxydm());
            djOrg.setDjxh(djNsrxx.getGszdjxh());
            djOrgMapper.insert(djOrg);

            *//*CompanyInfo companyInfo = new CompanyInfo();
            CompanyInfoDto
            BeanUtils.copyProperties(djNsrxx,);*//*
        }
    }*/

    @Override
    public void setOutlet(DjUserInfo djUserInfo) {
        LambdaQueryWrapper<SysDepart> query = Wrappers.lambdaQuery(SysDepart.class);
        query.eq(SysDepart::getOrgCode, djUserInfo.getSaleNetCode())
                .select(SysDepart::getDepartName);
        SysDepart sysDepart = sysDepartService.getOne(query);
        djUserInfo.setSaleNetName(sysDepart.getDepartName());
        LambdaUpdateWrapper<DjUserInfo> wrapper = Wrappers.lambdaUpdate(DjUserInfo.class);
        wrapper.eq(DjUserInfo::getNsrsbh, djUserInfo.getNsrsbh());
        djUserInfoMapper.update(djUserInfo, wrapper);
    }

    public DjCheckVO getUserInfoByNsrsbh(String nsrsbh) {
        return djUserInfoMapper.getUserInfoByNsrsbh(nsrsbh);
    }

    @Override
    public void setDjUser2SysUser(DjUserAssignRequest djUserAssignRequest) {
        LambdaQueryWrapper<DjUserInfo> wrapper = Wrappers.lambdaQuery(DjUserInfo.class);
        wrapper.select(DjUserInfo::getUserId).eq(DjUserInfo::getNsrsbh, djUserAssignRequest.getNsrsbh());
        djUserAssignRequest.setOrgId(djUserInfoMapper.selectOne(wrapper).getUserId());
        djUserInfoMapper.setDjUser2SysUser(djUserAssignRequest);
    }

    @Override
    public List<DjOrg> getCompanyList(String phone) {
        LambdaQueryWrapper<DjUserInfo> wrapper = Wrappers.lambdaQuery(DjUserInfo.class);
        wrapper.eq(DjUserInfo::getMobile, phone).select(DjUserInfo::getNsrsbh);
        List<Object> nsrsbhs = djUserInfoMapper.selectObjs(wrapper);
        List<DjOrg> list = new ArrayList<>();
        for (Object nsrsbh : nsrsbhs) {
            LambdaQueryWrapper<DjOrg> query = Wrappers.lambdaQuery(DjOrg.class);
            query.eq(DjOrg::getNsrsbh, nsrsbh).eq(DjOrg::getIsDelete, 0);
            DjOrg djOrg = djOrgMapper.selectOne(query);
            if (djOrg != null) {
                list.add(djOrg);
            }
        }
        return list;
    }

    @Override
    public void deleteCompany(String nsrsbh) {
        LambdaQueryWrapper<DjOrg> query = Wrappers.lambdaQuery(DjOrg.class);
        query.eq(DjOrg::getNsrsbh, nsrsbh);
        DjOrg djOrg = djOrgMapper.selectOne(query);
        djOrg.setIsDelete(true);
        djOrgMapper.updateById(djOrg);

        LambdaQueryWrapper<DjUserInfo> wrapper = Wrappers.lambdaQuery(DjUserInfo.class);
        wrapper.eq(DjUserInfo::getNsrsbh, nsrsbh);
        DjUserInfo djUserInfo = djUserInfoMapper.selectOne(wrapper);
        djUserInfo.setIsDelete(1);
        djUserInfoMapper.updateById(djUserInfo);
    }

    @Override
    public List<DjOrg> getFailCompanyList(String phone) {
        LambdaQueryWrapper<DjUserInfo> wrapper = Wrappers.lambdaQuery(DjUserInfo.class);
        wrapper.eq(DjUserInfo::getMobile, phone).select(DjUserInfo::getNsrsbh);
        List<Object> nsrsbhs = djUserInfoMapper.selectObjs(wrapper);
        List<DjOrg> list = new ArrayList<>();
        for (Object nsrsbh : nsrsbhs) {
            LambdaQueryWrapper<DjOrg> query = Wrappers.lambdaQuery(DjOrg.class);
            query.eq(DjOrg::getNsrsbh, nsrsbh).eq(DjOrg::getIsDelete, 1);
            DjOrg djOrg = djOrgMapper.selectOne(query);
            if (djOrg != null) {
                list.add(djOrg);
            }
        }
        return list;
    }
}
